package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.ArticleContstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2023/1/30 10:09
 * @Version 1.0
 */
@Service
public class ApArticleServiceImpl implements ApArticleService {


    public final static int DEAULT_SIZE = 10;
    //最大条数
    public final static int MAX_SIZE = 50;

    @Autowired
    private ApArticleMapper articleMapper;
    @Autowired
    private ApArticleConfigMapper configMapper;
    @Autowired
    private ApArticleContentMapper contentMapper;

    @Override
    public ResponseResult loadArticles(ArticleHomeDto dto, int type) {
        //参数校验
        Integer size = dto.getSize();
        if (size == null || size <= 0) {
            size = DEAULT_SIZE;
        }
        dto.setSize(Math.min(size, MAX_SIZE));
        //时间处理
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }
        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        if (StringUtils.isEmpty(dto.getTag())) {
            dto.setTag(ArticleContstants.ALL_TAG);
        }

        List<ApArticle> articles = articleMapper.loadArticles(dto, type);
        //查询
        return ResponseResult.okResult(articles);
    }

    @Transactional
    @Override
    public ResponseResult saveArticle(ArticleDto dto) {
        ApArticle article = new ApArticle();
        BeanUtils.copyProperties(dto, article);

        article.setPublishTime(new Date());

        //新增
        if (dto.getId() == null) {
            //article表
            articleMapper.insert(article);

            //保存文章配置表
            ApArticleConfig config = new ApArticleConfig(article.getId());
            configMapper.insert(config);

            //保存文章内容表
            ApArticleContent content = new ApArticleContent();
            content.setContent(dto.getContent());
            content.setArticleId(article.getId());
            contentMapper.insert(content);
        } else {
            //修改
            ApArticleContent articleContent = contentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, dto.getId()));
            articleContent.setContent(dto.getContent());
            contentMapper.updateById(articleContent);

            articleMapper.updateById(article);
        }

        return ResponseResult.okResult(article.getId());
    }

    @Override
    public ApArticle getArticleById(Long articleId) {
        return articleMapper.selectById(articleId);
    }
}